﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


    public class LogBLL
    {
        public static readonly object objLock = new object();
        public static void log(string msg)
        {
            string s = msg;
            //string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt";
            string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

            try
            {
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "log/");
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                if (!File.Exists(filename))
                {
                    StreamWriter sw = File.CreateText(filename + "\r\n");
                    sw.Close();
                }
            }
            catch (Exception)
            {
                //throw new Exception(ex.Message);
            }
            lock (objLock)
            {

                File.AppendAllText(filename, s + "\r\n");
                File.AppendAllText(filename, "time:" + DateTime.Now.ToString() + "\r\n");
                File.AppendAllText(filename, "----------------------------------" + "\r\n");

            }
        }

        public static void Err(string msg, Exception ex)
        {
            string s = "err:" + msg + ":\r\n" + ex.Message;


            string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt";
            try
            {
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "log/");
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);  

                if (!File.Exists(filename))
                {
                    StreamWriter sw = File.CreateText(filename);
                    sw.Close();
                }
            }
            catch (Exception)
            {
                //throw new Exception(ex.Message);
            }

            lock (objLock)
            {

                File.AppendAllText(filename, s + "\r\n");
                File.AppendAllText(filename, ex.StackTrace + "\r\n");
                File.AppendAllText(filename, "time:" + DateTime.Now.ToString() + "\r\n");
                File.AppendAllText(filename, "----------------------------------------------------------------------------------------------------------------" + "\r\n");

            }
        }
        public static void Err(string msg)
        {
            string s = msg;
            //string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt";
            string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

            try
            {
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "log/");
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);  

                if (!File.Exists(filename))
                {
                    StreamWriter sw = File.CreateText(filename + "\r\n");
                    sw.Close();
                }
            }
            catch (Exception)
            {
                //throw new Exception(ex.Message);
            }
            lock (objLock)
            {

                File.AppendAllText(filename, s + "\r\n");
                File.AppendAllText(filename, "time:" + DateTime.Now.ToString() + "\r\n");
                File.AppendAllText(filename, "----------------------------------" + "\r\n");

            }
        }
        public static void Err(string msg, string path)
        {
            string s = msg;
            string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd HH") + path + ".txt";

            try
            {
                if (!File.Exists(filename))
                {
                    StreamWriter sw = File.CreateText(filename + "\r\n");
                    sw.Close();
                }
            }
            catch (Exception)
            {
                //throw new Exception(ex.Message);
            }
            lock (objLock)
            {

                File.AppendAllText(filename, s + "\r\n");
                File.AppendAllText(filename, "time:" + DateTime.Now.ToString() + "\r\n");
                File.AppendAllText(filename, "----------------------------------" + "\r\n");

            }
        }
        //1 写入日志，2 弹出框 ，3 写入日志并且弹出框
        public static void Err(string msg, int type)
        {
            if (type == 1)
            {

                string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt";

                try
                {
                    if (!File.Exists(filename))
                    {
                        StreamWriter sw = File.CreateText(filename);
                        sw.Close();
                    }
                }
                catch (Exception)
                {
                    //throw new Exception(ex.Message);
                }
                lock (objLock)
                {

                    File.AppendAllText(filename, msg);
                    File.AppendAllText(filename, "time:" + DateTime.Now.ToString());
                    File.AppendAllText(filename, "----------------------------------");

                }
            }
            if (type == 2)
            {

            }
            if (type == 3)
            {

                string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-dd HH") + ".txt";

                try
                {
                    if (!File.Exists(filename))
                    {
                        StreamWriter sw = File.CreateText(filename);
                        sw.Close();
                    }
                }
                catch (Exception)
                {
                    //throw new Exception(ex.Message);
                }
                lock (objLock)
                {

                    File.AppendAllText(filename, msg);
                    File.AppendAllText(filename, "time:" + DateTime.Now.ToString());
                    File.AppendAllText(filename, "----------------------------------");

                }

            }


        }
    }

